package com.wjb.listener;

import com.rabbitmq.client.Channel;
import com.wjb.config.CouponClientExchangeConfig;
import com.wjb.controller.CouponController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author : 王济彬
 * @date: 2025/5/27 - 05 - 27 - 22:53
 * @Description: com.wjb.listener
 * @version: 1.0
 */
@Configuration
public class CouponListener {

    @Resource
    private CouponController couponController;

    private static final String MESSAGE_CONFIRM = "use_coupon";
    private static final Logger log = LoggerFactory.getLogger(CouponListener.class);
    private static final Logger log2 = LoggerFactory.getLogger(CouponController.class);

    @RabbitListener(queues = CouponClientExchangeConfig.CPUPON_QUEUE)
    public void consumer(String message, Channel channel, Message msg) throws IOException {
        log.info("couponListener接收到消息：{}",message);
        if(MESSAGE_CONFIRM.equals(message)){
            log2.info("couponController启动");
            couponController.useCoupon(1);
            //手动确认
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
        }

    }
}
